//Implementation file - Obstacle.cpp
#include "Segment.h"

Obstacle::Obstacle(  ) {
        vertexList = new Lista<PV>();
        coverVertexList = new Lista<PV>();
};

Obstacle::~Obstacle(  ) {
        delete vertexList;
        delete coverVertexList;
};

Lista<PV>* Obstacle::getVertexList(  ) {
        return this->vertexList;
};

Lista<PV>* Obstacle::getCoverVertexList(float width) {
        if (coverVertexList->vacia())
                renderCover(width);
        return this->coverVertexList;
};

void Obstacle::renderCover(float width) {
        if (!vertexList->vacia(  )) {
                vertexList->inicia();
                delete coverVertexList;
                coverVertexList = new Lista<PV>();

                PV previousNormal;
                // for each vertex, extend the side and build a round corner
                for (int i=0; i < vertexList->getLongitud(); i++) {
                        PV V1 = *vertexList->getActual(  );
                        // Return to initial vertex when last vertex
                        if (i==vertexList->getLongitud()-1) {
                                vertexList->inicia();
                        } else {
                                vertexList->avanza(  );
                        }
                        PV V2 = *vertexList->getActual(  );

                        // Actual polygon segment
                        PV V1_V2= V2.restaPV( V1 );
                        PV normal = V1_V2.normal();
                        normal.escalacion(width);


                        // Draw round corner
                        if (i>0) {
                                float angle = normal.alpha() - previousNormal.alpha();
                                if (angle<0) angle = 2*M_PI + angle;
                                angle = angle/5;
                                for (int j=0; j<5; j++) {
                                        previousNormal.rotaPV(angle);
                                        coverVertexList->insertaDespues(new PV(V1.sumaPV(previousNormal)));
                                }
                        }

                        // Extend side
                        coverVertexList->insertaDespues(new PV(V1.sumaPV(normal)));
                        coverVertexList->insertaDespues(new PV(V2.sumaPV(normal)));

                        previousNormal = normal;

                        // If this is the last vertex, draw last corner
                        if (i==vertexList->getLongitud()-1) {
                                vertexList->avanza();
                                PV V3 = *vertexList->getActual();
                                PV V2_V3= V3.restaPV( V2 );
                                PV nextNormal = V2_V3.normal();
                                nextNormal.escalacion(width);
                                float angle = nextNormal.alpha() - normal.alpha();
                                if (angle<0) angle = 2*M_PI + angle;
                                angle = angle/5;
                                for (int j=0; j<5; j++) {
                                        normal.rotaPV(angle);
                                        coverVertexList->insertaDespues(new PV(V2.sumaPV(normal)));
                                }
                        }

                }
        }
};
